﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Frame.Lib.Core.Base;
using Frame.Lib.Core.Module;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;
using Frame.Lib.Web;
using Frame.Lib.Core.Log;


namespace Frame.Lib.Core.Role
{
    public class RoleBO : CommonOperate<RoleInfo>, IModuleAccess
    {
        private RoleDAL _role = new RoleDAL();

        protected override BaseDAL DAL
        {
            get
            {
                return _role;
            }
        }

        public RoleBO() : base(STableName.Role.ToString()) { }

        #region IModuleAccess 成员

        public List<int> GetModuleIDs(int roleID)
        {
            List<int> lstModuleIDs = _role.GetModuleIDs(roleID);
            return lstModuleIDs;
        }

        public List<int> GetModuleIDs(IEnumerable<int> lstRoleIDs)
        {
            List<int> lstModuleIDs = _role.GetModuleIDs(lstRoleIDs);
            return lstModuleIDs;
        }

        public void SaveModuleIDs(int roleID, List<int> lstModuleIDs)
        {
            _role.SaveModuleIDs(roleID, lstModuleIDs);
        }

        public void SaveModuleIDs(List<int> lstRoleIDs, int moduleID)
        {
            _role.SaveModuleIDs(lstRoleIDs, moduleID);
        }

        #endregion

        //public void SaveUserDefaultRole(IEnumerable<int> lstRoleIDs)
        //{
        //    role.SaveUserDefaultRole(lstRoleIDs);
        //}

        //public void SaveModuleDefaultRole(IEnumerable<int> lstRoleIDs)
        //{
        //    role.SaveModuleDefaultRole(lstRoleIDs);
        //}
        public void SaveUserIDs(int roleID, List<int> lstUserIDs)
        {
            _role.SaveUserIDs(roleID, lstUserIDs);
        }

        public void SaveUserIDs(List<int> lstRoleIDs, int userID)
        {
            _role.SaveUserIDs(lstRoleIDs, userID);
        }

        public List<int> GetUserIDs(int roleID)
        {
            List<int> lstUserIDs = _role.GetUserIDs(roleID).ToList();
            return lstUserIDs;
        }

        public List<int> GetUserIDs(List<int> roleIDs)
        {
            List<int> lstUserIDs = _role.GetUserIDs(roleIDs);
            return lstUserIDs;
        }

        private void InsertInternalRole(List<RoleInfo> list)
        {
            List<EnumInfo> enumInfos = ConvertUtil.ConvertEnum(typeof(InternalRole)).OrderByDescending(T => T.ID).ToList();

            try
            {
                if (!string.IsNullOrEmpty(AppSetting.SystemRole))
                {
                    Type type = Type.GetType(AppSetting.SystemRole);
                    if (type != null)
                    {
                        enumInfos.AddRange(ConvertUtil.ConvertEnum(type));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
            }

            foreach (EnumInfo tmp in enumInfos)
            {
                RoleInfo info = new RoleInfo();
                info.ID = tmp.ID;
                info.Name = tmp.Name;
                info.TypeID = (int)RoleType.内置;
                list.Insert(0,info);
            }
        }

        public override List<RoleInfo> GetList(PagerSortSetting setting)
        {
            List<RoleInfo> list = this.GetList();
            setting.RecordCount = list.Count();
            return list.Skip(setting.SkipCount).Take(setting.PageSize).ToList();
        }

        /// <summary>
        /// 获得一批类型的角色
        /// </summary>
        /// <param name="roleTypes">如果数组个数为0则返回全部类型的角色</param>
        /// <returns></returns>
        public List<RoleInfo> GetList(params RoleType[] roleTypes)
        {
            List<RoleInfo> list = null;
            if (roleTypes.Length == 0)
            {
                list = base.GetList().ToList();
                InsertInternalRole(list);
            }
            else
            {
                list = _role.GetList(roleTypes);
                if (roleTypes.Contains(RoleType.内置))
                {
                    InsertInternalRole(list);
                }
            }

            return list;
        }

        public override void Delete(IEnumerable<int> ids)
        {
            _role.Delete(ids);
        }


        internal void AddModuleID(int roleID, int moduleID)
        {
            _role.AddModuleID(roleID, moduleID);
        }

        internal void AddUserID(int roleID, int userID)
        {
            _role.AddUserID(roleID, userID);
        }
    }
}
